//
//  WCOrderListVC.m
//  CBT
//
//  Created by apple on 2019/7/8.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "WCOrderListVC.h"
#import "UserManager.h"
#import "WCOrderCompletedModel.h"
#import "WCOrderListCell.h"

@interface WCOrderListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) int page;
@property (nonatomic, strong) NSMutableArray *contents;

@end

@implementation WCOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = kLocalizedString(@"我的订单",nil);
    
    _page = 1;
    _contents = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 190;
    [self.tableView registerClass:[WCOrderListCell class] forCellReuseIdentifier:@"orderlistCell"];
    [self getRecordList];
}

- (void)getRecordList {
    @weak(self)
    [UserManager getUserOrderRecordWithUserId:[CBTLocalUserManager sharedInstance].userId page:[NSString stringWithFormat:@"%d",_page] block:^(id data, HttpRequestResult result, NSString *msg) {
        if (result == kHttpRequestResultNormal) {
            NSDictionary *dic = data[@"data"];
            NSArray *contents = dic[@"invitedList"];
            if (self_weak_.page == 1) {
                [self_weak_.contents removeAllObjects];
            }
            else {
                if (contents.count == 0) {
                    self_weak_.tableView.mj_footer.state = MJRefreshStateNoMoreData;
                }
            }
            if (contents) {
                for (NSDictionary *sub in contents) {
                    WCOrderCompletedModel *model = [WCOrderCompletedModel mj_objectWithKeyValues:sub];
                    [self_weak_.contents addObject:model];
                }
            }
            [self_weak_.tableView reloadData];
        }
        else {
            [HFStatusView showFailureMessage:kLocalizedString(@"网络异常，请稍后重试", nil)];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contents.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    WCOrderListCell *cell = [WCOrderListCell createTableView:tableView];
    cell.model = self.contents[indexPath.row];
    
    return cell;
}

- (UITableView *)tableView {
    if (!_tableView) {
        CGRect rect = self.view.frame;
        rect.size.height -= HFNavigationHeight;
        _tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
        
        _tableView.delegate     = self;
        _tableView.dataSource   = self;
        _tableView.scrollsToTop = YES;
        
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
        
        if (@available(iOS 11.0, *)){
            self.tableView.estimatedRowHeight = 0;
            self.tableView.estimatedSectionHeaderHeight = 0;
            self.tableView.estimatedSectionFooterHeight = 0;
        }
        
        __weak WCOrderListVC *weakSelf = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.page = 1;
            self.tableView.mj_footer.state = MJRefreshStateIdle;
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf getRecordList];
        }];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            weakSelf.page++;
            [weakSelf getRecordList];
            [weakSelf.tableView.mj_footer endRefreshing];
        }];
        
        ((MJRefreshAutoNormalFooter *)_tableView.mj_footer).triggerAutomaticallyRefreshPercent = 50;
        ((MJRefreshAutoNormalFooter *)_tableView.mj_footer).automaticallyRefresh = NO;
        ((MJRefreshAutoNormalFooter *)_tableView.mj_footer).onlyRefreshPerDrag = YES;
    }
    return _tableView;
}


@end
